package com.xt.core.helper.page;

import java.util.LinkedHashMap;
import java.util.Set;
import java.util.HashSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xt.core.BaseConstants;
import com.xt.core.helper.data.HibernateFormatUtil;

/**
 * a help class for pager properties in action
 * can be used in modeldriven action
 * contains commen properties for pagenation, for example
 * 1. sort
 * 2. search query
 * 3. pager 
 * 
 * url must be like list.action?page=1
 * 
 * @author txiao
 * @version Revision: 1.0, Date: 02.11.2009
 */
public class PageForm {

	protected final Log log = LogFactory.getLog(getClass());
	
	private int totalRows;
	private String page; //open the page directly
	private Pager pager;
	private LinkedHashMap<String, String> order = new LinkedHashMap<String, String>();
	private Set<LinkedHashMap<String, Object>> queryParams = new HashSet<LinkedHashMap<String, Object>>();
	
	public PageForm(){
	
	}
	
	public PageForm(int totalRows){
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(page, totalRows);
		this.page=null; //TODO reset the page number in url, so without page= go back only to the first page
	}
	
	public PageForm(int totalRows, int pageSize){
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(page, totalRows);
		this.pager.setPageSize(pageSize);
		this.page=null; //TODO reset the page number in url, so without page= go back only to the first page
	}
	
	public void refresh(int totalRows){
		//log.debug("pageform refresh: totalrows: " + totalRows + " currentPage: " + currentPage + " method: " + pagerMethod);
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(page, totalRows);
		this.page=null; //TODO reset the page number in url, so without page= go back only to the first page
	}
	
	/**
	 * for the case pagesize from the user
	 * 
	 * @param totalRows
	 * @param pageSize
	 */
	public void refresh(int totalRows, int pageSize){
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(page, totalRows);
		this.pager.setPageSize(pageSize);
	}
	
	/**
	 * reset the query parameters
	 */
	public void resetQuery(){
		this.queryParams.clear();
	}
	
	public void resetOrder(){
		this.order.clear();
	}
		
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public int getTotalRows() {
		return totalRows;
	}	
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}	
	public String getNextPage() {
		int i = pager.getCurrentPage()+1;
		if(i>pager.getTotalPages())
			i=pager.getTotalPages();
		return new Integer(i).toString();
	}
	public String getPreviousPage() {
		int i = pager.getCurrentPage()-1;
		if(i<1)
			i=1;
		return new Integer(i).toString();
	}
	public Pager getPager() {
		return pager;
	}
	public LinkedHashMap<String, String> getOrder() {
		return order;
	}
	public void setOrder(LinkedHashMap<String, String> order) {
		this.order = order;
	}
	public void addOrder(String name, String value){
		this.order.put(name,value);
	}
	public Set<LinkedHashMap<String, Object>> getQueryParams() {
		return queryParams;
	}
	/*
	public void setQueryParams(Set<LinkedHashMap<String, Object>> queryParams) {
		this.queryParams = queryParams;
	}
	*/
	public void addAliasParam(String property, String alias){
		HibernateFormatUtil.addOneCondition(queryParams, property, alias, BaseConstants.TYPE_ALIAS);
	}
	
	public void addEqParam(String name, Object value){
		HibernateFormatUtil.addOneCondition(this.queryParams, name, value, BaseConstants.TYPE_EQ);
	}
	public void addLikeParam(String name, Object value){
		HibernateFormatUtil.addOneCondition(this.queryParams, name, value, BaseConstants.TYPE_LIKE);
	}
	
	public void addGeParam(String name, Object value){
		HibernateFormatUtil.addOneCondition(this.queryParams, name, value, BaseConstants.TYPE_GE);
	}
	
	public void addLeParam(String name, Object value){
		HibernateFormatUtil.addOneCondition(this.queryParams, name, value, BaseConstants.TYPE_LE);
	}

	/**
	 * add a  "or" query
	 * the whole set is a "or" disjunction
	 * every set value composed by three attributes: value, fieldName, condition type
	 * 
	 * @param crit
	 * @param queryParams
	 * @return
	 */
	public void addOrParam(String name, Set<LinkedHashMap<String, Object>> oneOrCondition){
		HibernateFormatUtil.addOneCondition(this.queryParams, name, oneOrCondition, BaseConstants.TYPE_OR);
	}
	
	/**
	 * !!!!under contruction!!!
	 * 
	 * @param name
	 * @param sqlRestrictionString
	 * @param values
	 * @param types
	public void addSqlRestriction(String name, String sqlRestrictionString, Object[] values, Type[] types){

		LinkedHashMap<String, Object> oneRestrict = new LinkedHashMap<String, Object>();
		oneRestrict.put(BaseConstants.SQLRESTRICTIONSTRING, sqlRestrictionString);
		oneRestrict.put(BaseConstants.SQLRESTRICTIONVALUES, values);
		oneRestrict.put(BaseConstants.SQLRESTRICTIONTYPES, types);
		
		this.sqlRestrictionParams.put(name, oneRestrict);
	}
	 */	
	
}
